package the.workshop.bu.dto;

import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import the.workshop.bu.entity.Credit;
import the.workshop.bu.entity.CreditProduct;

import java.util.Date;
import java.util.List;

@Data
public class CreditAddDTO {

    /**
     * 客户ID
     */
    private Integer customerId;

    /**
     * 业务员ID
     */
    private String salesId;

    /**
     * 备注
     */
    private String remark;

    /**
     * 申贷状态
     */
    private String status;

    /**
     * 创建人ID
     */
    private String createdId;

    /**
     * 创建人姓名
     */
    private String createdName;

    private List<CreditProduct> products;

    public Credit toCredit() {
        Credit credit = new Credit();
        BeanUtils.copyProperties(this, credit);
        credit.setCreatedTime(new Date());
        credit.setModifiedTime(new Date());
        return credit;
    }

    public List<CreditProduct> getProducts(Integer creditId) {
        if (CollectionUtils.isEmpty(products)) {
            return null;
        }

        for (CreditProduct cp : products) {
            cp.setCreditId(creditId);
            cp.setCustomerId(this.customerId);
            cp.setCreatedId(this.createdId);
            cp.setCreatedName(this.createdName);
            cp.setCreatedTime(new Date());
        }
        return products;
    }

}
